<?php
include_once 'GmailOath.php';
require_once('../../../../../config.php');

$cuncumer_key='238913647722.apps.googleusercontent.com';
$cunsumer_secret='XyWW8ClckySq74IENeZPy8sy';
$argarray = $argv[0];
$debug = 0; // Set to 1 for verbose debugging output
$callback = HTTP_SERVER . 'catalog/controller/account/plugin/Google/GetContact.php';

//oauth_token=kke8tht&oauth_verifier=cxectp

$oauth =new GmailOath($cuncumer_key, $cunsumer_secret, $argarray, $debug, $callback);

$getcontact_access=new GmailGetContacts();

session_start();
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// Get the access token using HTTP GET and HMAC-SHA1 signature

$request_token=$oauth->rfc3986_decode($_GET['oauth_token']);
//echo "Request token :".$request_token;
//echo "<br />";

//$request_token='k57356v';
$request_token_secret=$oauth->rfc3986_decode($_SESSION['oauth_token_secret']);
//echo "Request token secret :".$request_token_secret;
//echo "<br />";

//$oauth_verifier= 'pkzkns';
$oauth_verifier= $oauth->rfc3986_decode($_GET['oauth_verifier']);
//echo "Request token verifier :".$oauth_verifier;
//echo "<br />";

//echo "access token info<br />";


$retarr = $getcontact_access->get_access_token($oauth,
                           $request_token, $request_token_secret,
                           $oauth_verifier, false, true, true);

////////////////////////////////////////////////////////////////////
// Call Contact API

//echo "<br />";


$access_token=$oauth->rfc3986_decode($retarr['oauth_token']);
//echo "Access token :".$access_token;
//echo "<br />";

$access_token_secret=$oauth->rfc3986_decode($retarr['oauth_token_secret']);
//echo "access token sectet :".$access_token_secret;
//echo "<br />";

$contacts	=	$getcontact_access->callcontact($oauth, $access_token, $access_token_secret, false, true);

	//Modify by Nguyen Hai Dang 03/10/2012
	
	//Connect database
	$link = mysql_connect(DB_HOSTNAME,DB_USERNAME , DB_PASSWORD);
	if(!$link)
		die('Not connected : ' . mysql_error());
	$db_selected = mysql_select_db(DB_DATABASE, $link);
	if (!$db_selected)
		die('Can\'t select : ' . mysql_error());
	
	//insert email of persion login into 99xu
	$mainemail = $_SESSION['mainemail'];
	$sql = mysql_query("SELECT emailaddress FROM email_list_subscribers WHERE emailaddress='$mainemail' AND listid='97'") or die(mysql_error());
	$numRows = mysql_num_rows($sql);
	if($numRows == 0) {
		mysql_query("INSERT INTO email_list_subscribers (listid,emailaddress,format,confirmed,confirmcode)
												VALUES ('97','$mainemail','h','1','9a69e28c1a48e5c35ca9c0865ee0034f')");
	}

	//insert email friendlist
	if(!empty($contacts))
	{
		foreach($contacts as $key => $val)
		{
			$final = end($contacts[$key]);
			foreach($final as $email)
			{
				$value=$email["address"];
				$sql = mysql_query("SELECT emailaddress FROM email_list_subscribers WHERE emailaddress='$value' AND listid='97'") or die(mysql_error());
				$numRows = mysql_num_rows($sql);
				if($numRows == 0) {
					mysql_query("INSERT INTO email_list_subscribers (listid,emailaddress,format,confirmed,confirmcode)
												VALUES ('97','$value','h','1','9a69e28c1a48e5c35ca9c0865ee0034f')");
				}
			}
		}
	}
	
	//Update number of email OPENID
    $sql1_total = mysql_query("SELECT emailaddress FROM email_list_subscribers WHERE listid='97'");
    $numRows_total1 = mysql_num_rows($sql1_total);
	$sql2_total = mysql_query("UPDATE email_lists SET subscribecount=$numRows_total1, agreedelete='1'  WHERE listid='97'");
//End modify--------------	
mysql_close($link);
header('Location:'.HTTP_SERVER. 'index.php');
?>
